// pages/movie/movie.js
import resource from "../../libs/resource.js"

Page({
  data: {
    loading: true,
    subjects: [],
    top_subs: [],
    swiper: {
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    },
    swiper_itemId: 0,
    top250_pic: ""
  },

  onLoad: function (options) {
    this.setData({
      onling: getApp().globalData.onling
    })
    if (this.data.onling) return
    this.getMovieShowing();
    this._get_movie_pic();
  },
  onShareAppMessage() {

  },
  // 获取正在热映的电影
  getMovieShowing: function (city) {
    wx.showLoading({
      title: '加载中',
    })
    var _this = this;
    wx.cloud.callFunction({
      name: 'movie',
      data: {
        action: 'getMovieShowing'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        var movieObj = res.result.data;
        console.log("一条电影数据", movieObj[0])
        _this.setData({
          loading: false,
          top_subs: movieObj.slice(0, 5),
          swiper_itemId: movieObj[0].subject
        })
      },
      fail: console.error
    })
  },
  //swiper轮播事件
  swiperChange: function (event) {
    console.log(event)
    this.setData({
      swiper_itemId: event.detail.currentItemId
    })
  },
  //轮播图点击事件
  swiperClick: function (event) {
    console.log(event)
    var subject = event.currentTarget.dataset.subject
    console.log(subject)
    wx.navigateTo({
      url: "../movie/movieDetail/movieDetail?subject=" + subject,
    })
  },
  //获取图片的链接
  _get_movie_pic: function () {
    wx.cloud.getTempFileURL({
      fileList: [resource.movie_top250_pic],
      success: res => {
        // fileList 是一个有如下结构的对象数组
        // [{
        //    fileID: 'cloud://xxx.png', // 文件 ID
        //    tempFileURL: '', // 临时文件网络链接
        //    maxAge: 120 * 60 * 1000, // 有效期
        // }]
        console.log(res)
        console.log(res.fileList[0])
        this.setData({
          top250_pic: res.fileList[0].tempFileURL
        })
      },
      fail: console.error
    })
  },
  //电影Top250点击
  _top250_click: function () {
    wx.navigateTo({
      url: "../movie/movie250/movie250",
    })
  }
})